﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Wicresoft.RDW.Presentation
{
    public class IOC
    {
        protected static IDictionary<string, object> InstanceDic = new Dictionary<string, object>();
        protected static object CreateInstance(string typeName)
        {
            string assemblyName = "Wicresoft.RDW.Business";
            string targetTypeName = typeName.Split('.').Last().Substring(1);
            Assembly assembly = Assembly.Load(assemblyName);
            object obj = assembly.CreateInstance(string.Format("{0}.{1}Biz",assemblyName,targetTypeName));
            return obj;
        }
        public static T Load<T>()
        { 
            string typeName=typeof(T).ToString();
            if (!InstanceDic.ContainsKey(typeName))
            { 
                object instance=CreateInstance(typeName);
                if (instance != null)
                {
                    InstanceDic.Add(typeName, instance);
                }
                else
                {
                    throw new Exception(string.Format("Type:{0} can not be loaded.",typeName));
                }
            }
            return (T)InstanceDic[typeName];   
        }
    }
}
